package com.snow.datasource.creator.druid;

import cn.hutool.core.util.ObjectUtil;
import lombok.Data;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.lang.reflect.Field;
import java.util.Properties;

/**
 * druid数据源属性
 *
 * @Description
 * @Author fanxl
 * @Date 2020/12/17
 **/
@Data
public class DruidProperties {
    public static final String PREFIX = "spring.datasource.druid";
    private Integer initialSize;
    private Integer maxActive;
    private Integer minIdle;
    private Integer maxWait;
    private Long timeBetweenEvictionRunsMillis;
    private Long timeBetweenLogStatsMillis;
    private Integer statSqlMaxSize;
    private Long minEvictableIdleTimeMillis;
    private Long maxEvictableIdleTimeMillis;
    private String defaultCatalog;
    private Boolean defaultAutoCommit;
    private Boolean defaultReadOnly;
    private Integer defaultTransactionIsolation;
    private Boolean testWhileIdle;
    private Boolean testOnBorrow;
    private Boolean testOnReturn;
    private String validationQuery;
    private Integer validationQueryTimeout;
    private Boolean useGlobalDataSourceStat;
    private Boolean asyncInit;
    private Boolean clearFiltersEnable;
    private Boolean resetStatEnable;
    private Integer notFullTimeoutRetryCount;
    private Integer maxWaitThreadCount;
    private Boolean failFast;
    private Long phyTimeoutMillis;
    private Boolean keepAlive;
    private Boolean poolPreparedStatements;
    private Boolean initVariants;
    private Boolean initGlobalVariants;
    private Boolean useUnfairLock;
    private Boolean killWhenSocketReadTimeout;
    private Properties connectionProperties;
    private Integer maxPoolPreparedStatementPerConnectionSize;
    private String initConnectionSqls;
    private Boolean sharePreparedStatements;
    private Integer connectionErrorRetryAttempts;
    private Boolean breakAfterAcquireFailure;
    private Boolean removeAbandoned;
    private Integer removeAbandonedTimeoutMillis;
    private Boolean logAbandoned;
    private Integer queryTimeout;
    private Integer transactionQueryTimeout;
    private String publicKey;

    @NestedConfigurationProperty
    private FiltersConfig filter = new FiltersConfig();

    public static Properties toProperties(DruidProperties object){
        Properties properties = new Properties();

        Field[] fields =  object.getClass().getDeclaredFields();
        for (Field field : fields){
            field.setAccessible(true);
            try {
                if(field.get(object) != null){
                    properties.put("druid."+field.getName(),field.get(object));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }
        if(ObjectUtil.isNotNull(object.getFilter())){
            if(ObjectUtil.isNotNull(String.valueOf(object.getFilter().getStat()))){
                properties.setProperty("druid.filters", "stat,wall");
                if(ObjectUtil.isNotNull(object.getFilter().getStat().getLogSlowSql())){
                    properties.setProperty("druid.stat.logSlowSql",String.valueOf(object.getFilter().getStat().getLogSlowSql()));
                }
                if(ObjectUtil.isNotNull(object.getFilter().getStat().getSlowSqlMillis())){
                    properties.setProperty("druid.stat.slowSqlMillis",String.valueOf(object.getFilter().getStat().getSlowSqlMillis()));
                }
                if(ObjectUtil.isNotNull(object.getFilter().getStat().getMergeSql())){
                    properties.setProperty("druid.stat.mergeSql",String.valueOf(object.getFilter().getStat().getMergeSql()));
                }
            }
        }
        return properties;
    }
}
